#include <rtthread.h>

#include "mb.h"
#include "user_mb_app.h"

#define MB_POLL_THREAD_PRIORITY  10
#define MB_POLL_CYCLE_MS 5

static void mb_slave_poll(void *parameter)
{
    eMBTCPInit(MB_TCP_PORT_USE_DEFAULT);
    eMBEnable();
    while (1)
    {
        eMBPoll();
        rt_thread_mdelay(MB_POLL_CYCLE_MS);
    }
}

static int modbus_init(void)
{
    rt_thread_t tid1 = RT_NULL;

    tid1 = rt_thread_create("md_tcp_poll", mb_slave_poll, RT_NULL, 2048, MB_POLL_THREAD_PRIORITY, 10);
    if (tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }
    else
    {
        goto __exit;
    }

    return RT_EOK;

__exit:
    if (tid1)
        rt_thread_delete(tid1);

    return -RT_ERROR;
}
INIT_APP_EXPORT(modbus_init);
